/**
 * scroll想关共用工具方法
 *
 * desc：规律 当滚动条滚到底部是满足： getScrollTop()+getWindowHeight() = getScrollHeight()
 *
 * Created by zoushuiyun on 2017/7/3.
 */
/**
 * 滚动条距离顶部的距离
 *
 * @returns {number}
 */
export function getScrollTop(){
  var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
  if(document.body){
    bodyScrollTop = document.body.scrollTop;
  }
  if(document.documentElement){
    documentScrollTop = document.documentElement.scrollTop;
  }
  scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
  return scrollTop;
}

/**
 * 滚动条高度
 *
 * @returns {number}
 */
export function getScrollHeight(){
  var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
  if(document.body){
    bodyScrollHeight = document.body.scrollHeight;
  }
  if(document.documentElement){
    documentScrollHeight = document.documentElement.scrollHeight;
  }
  scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
  return scrollHeight;
}

/**
 * 当前窗口的高度
 *
 * @returns {number}
 */
export function getWindowHeight(){
  var windowHeight = 0;
  if(document.compatMode == "CSS1Compat"){
    windowHeight = document.documentElement.clientHeight;
  }else{
    windowHeight = document.body.clientHeight;
  }
  return windowHeight;
}

/**
 * 是否就要接近底部了 是返回true 否 返回 false ;
 * desc:
 * 此方法认为 当滚动条距离底部的距离小于当前窗口的高度时 则为就要接近底部了，即可继续做分页查下下页等业务逻辑操作
 *
 * 原理： 当滚动条滚到底部是满足： getScrollTop()+getWindowHeight() = getScrollHeight()
 *
 * @returns {number}
 */
export function approachToBottom():boolean{
  return (getScrollHeight()-2*getWindowHeight())< getScrollTop();
}
